<?php

use yii\helpers\Html;
use yii\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model common\models\business\BusinessContractPurchase */

$this->title = $model->name;
$this->params['breadcrumbs'][] = ['label' => 'Business Contract Purchases', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;

use backend\assets\PrintAsset;
PrintAsset::register($this);

$costtotal =0;
?>


<section class="wrapper site-min-height">
    <!-- page start-->
    <section class="panel">
        <header class="panel-heading">
              <?= Html::a('返回','javascript:history.back(-1)'); ?>  <?= Html::encode($this->title) ?>
        </header>
        <div class="panel-body">
            <div class="adv-table editable-table ">

                <div class="business-contract-purchase-view">

                    <p>
                        <?= Html::button('打印', ['class' => 'btn btn-info','data-toggle'=>"modal",'data-target'=>"#printmodel"]) ?>
                        <?= Html::a('更新', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
                        <?= Html::a('删除', ['delete', 'id' => $model->id], [
                            'class' => 'btn btn-danger',
                            'data' => [
                                'confirm' => '您确定删除此项吗?',
                                'method' => 'post',
                            ],
                        ]) ?>
                    </p>

                    <?= DetailView::widget([
                        'model' => $model,
                        'attributes' => [
                            'id',
                            [
                                'attribute' => 'contract_id',
                                'value' => function($model){
                                    return $model->contract?$model->contract->name:'';
                                }
                            ],
                            'name',
                            'num',
                            [
                                'attribute' => 'createtime',
                                'value' => function($model){
                                    return date('Y-m-d H:i:s',$model->createtime);
                                }
                            ],
                            'price',
                            [
                                'attribute'=>'pigs',
                                'format'=>'raw',
                                'value'=>function($model){
                                    $html = '';
                                    foreach ($model->pigs as $key => $row){
                                        $html .= Html::label($row->no,'',['class'=>'btn btn-default btn-xs','style'=>'margin-right:10px;']);
                                    }
                                    return $html;
                                }
                            ],
                        ],
                    ]) ?>

                    <!--包含的仔猪-->
                    <div class="row">
                        <div class="col-md-8 col-md-offset-2">
                            <table class="table table-striped" id="cost">
                                <tr>
                                    <th>仔猪</th>
                                    <th >收购价</th>
                                    <th >状态</th>
                                </tr>
                                <?php foreach ($model->pigs as $item => $value)  { ?>
                                    <?php $costtotal += $value['cost'] ?>
                                    <tr class="costdata">
                                        <td>
                                            <span><?= $value['no'] ?></span>
                                        </td>
                                        <td>
                                            <span><?= $value['cost'] ?></span>
                                        </td>
                                        <td>
                                            <span><?= \common\models\business\BusinessPig::$statues[$value['status']] ?></span>
                                        </td>
                                    </tr>
                                <?php } ?>

                                <tr class="costdata" id="zongji">
                                    <td><b>总计</b></td>
                                    <td><b><?= $costtotal ?></b></td>
                                </tr>
                            </table>
                        </div>
                    </div>


                    <!--打印模态窗-->
                    <div class="modal fade  bs-example-modal-lg" id="printmodel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                    <h4 class="modal-title" id="kucuntitle">现有库存</h4>
                                </div>
                                <div class="modal-body">
                                    <div class="row">
                                        <!--打印结算明细区域-->
                                        <div id="printarae" class="col-md-12;">


                                            <h4 style="margin-bottom: 10px;text-align: center">购进单</h4>

                                            <div class="col-md-10 col-md-offset-1">
                                                <span style="width:50%;display: block;float: left;">乙方:<?= $model->contract->partyB ?></span>
                                                <span style="width: 50%;display: block;float: right;">购进时间：<?= date('Y-m-d',$model->createtime) ?></span>
                                            </div>
                                            <div class="col-md-10 col-md-offset-1">
                                                <span style="width: 50%;display: block;float: left;">合同编号:<?= $model->contract->no ?></span>
                                                <span style="width: 50%;display: block;float: left;">联系人:<?= $model->connect_person ?></span>
                                            </div>
                                            <div class="col-md-10 col-md-offset-1">
                                                <span style="width:50%;display: block;float: left;">联系地址:<?= $model->connect_address?></span>
                                                <span style="width: 50%;display: block;float: left;">联系电话:<?= $model->connect_tel?></span>
                                            </div>
                                            <div class="col-md-10 col-md-offset-1">
                                                <span style="width: 50%;display: block;float: left;">购进金额:<?= $model->price ?></span>
                                                <span style="width: 50%;display: block;float: right;">结算情况：<?php if($model->sta_id == 0){ ?>未结算<?php }else {?>已结算<?php } ?></span>
                                            </div>
                                            <div class="col-md-10 col-md-offset-1">
                                                <table class="table table-bordered">
                                                    <tr>
                                                        <th>仔猪编号</th>
                                                        <th>购进价格</th>
                                                    </tr>

                                                    <?php
                                                    foreach ($model->pigs as $key => $row) {?>
                                                        <tr>
                                                            <td><?= $row['no'] ?></td>
                                                            <td><?= $row['cost'] ?>元</td>
                                                        </tr>
                                                    <?php } ?>
                                                    <tr>
                                                        <td>总计</td>
                                                        <td><?= $costtotal ?>元</td>
                                                    </tr>

                                                </table>
                                            </div>
                                            <div class="col-md-10 col-md-offset-1">
                                                <div class="col-md-12">
                                                    <div>总计：<?= $model->price  ?> 元</div>
                                                </div>
                                                <div class="col-md-12">
                                                    <div style="width:35%;float: left;">制单人:<?= $model->inputuser ?></div>
                                                    <div style="width:35%;float: left;">审核人:<?= $model->agreeuser?$model->agreeuser:'未审核' ?></div>
                                                    <div style="width:30%;float: right;">审核日期:<?= $model->agreetime?date('Y-m-d',$model->agreetime):'' ?></div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="modal-footer">
                                    <button class="btn btn-primary" id="print">打印</button>
                                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </section>
</section>


<script>
    <?php $this->beginBlock('js_end') ?>

    //打印
    $(function(){
        $("#print").printPreview({
            obj2print:'#printarae',
            width:'810'
        });
    });

    <?php $this->endBlock('jsend') ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>

</script>
